#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "TextureBox.h"

TextureBox::TextureBox()
{
	tile = NULL;
	rotation = 0;
	for(int i = 0; i<4; i++)
		holdingControlPoint[i]=false;
	this->size = Vector2D(1,1);
	float halfX = size.x/2;
	float halfY = size.y/2;
	vertices[0] = Vertex(Vector2D(-halfX, -halfY));
	vertices[1] = Vertex(Vector2D(-halfX, halfY));
	vertices[2] = Vertex(Vector2D(halfX, halfY));
	vertices[3] = Vertex(Vector2D(halfX, -halfY));
		
}
TextureBox::TextureBox( Tile * tile, Vector2D size)
{
	this->tile = tile;
	
	for(int i = 0; i<4; i++)
		holdingControlPoint[i]=false;
	this->size = size;
	
	float halfX = size.x/2;
	float halfY = size.y/2;
	vertices[0] = Vertex(Vector2D(-halfX, halfY));
	vertices[1] = Vertex(Vector2D(halfX, halfY));
	vertices[2] = Vertex(Vector2D(halfX, -halfY));
	vertices[3] = Vertex(Vector2D(-halfX, -halfY));
}
TextureBox::~TextureBox()
{
	if(tile)
	{
		delete(tile);
		tile = NULL;
	}
}

void TextureBox::Draw()
{
	if(tile)
		tile->Draw(rotation, vertices);
	//else
	//{
	glDisable(GL_TEXTURE_2D);
	glColor3f(.4,.4,.4);
	glBegin(GL_LINE_LOOP);
	for(int i=0; i < 4; i++)
  	{
  		glVertex2f(vertices[i].GetPosition().x, vertices[i].GetPosition().y);
  	}
	glEnd();
	float controlSize = 0.1;
	glBegin(GL_QUADS);
	glVertex2f(-size.x/2 - controlSize, controlSize);
	glVertex2f(-size.x/2 + controlSize, controlSize);
	glVertex2f(-size.x/2 + controlSize, -controlSize);
	glVertex2f(-size.x/2 - controlSize, -controlSize);
	
	glVertex2f(size.x/2 - controlSize, controlSize);
	glVertex2f(size.x/2 + controlSize, controlSize);
	glVertex2f(size.x/2 + controlSize, -controlSize);
	glVertex2f(size.x/2 - controlSize, -controlSize);
	
	glVertex2f(-controlSize, size.y/2 + controlSize);
	glVertex2f(controlSize, size.y/2 + controlSize);
	glVertex2f(controlSize, size.y/2-controlSize);
	glVertex2f(-controlSize, size.y/2-controlSize);
	
	glVertex2f(-controlSize, -size.y/2 + controlSize);
	glVertex2f(controlSize, -size.y/2 + controlSize);
	glVertex2f(controlSize, -size.y/2-controlSize);
	glVertex2f(-controlSize, -size.y/2-controlSize);
	
	glEnd();
	//}
	
}
bool TextureBox::Click(Vector2D spot)
{
	Vector2D controlPoints[4] = {Vector2D(-size.x/2,0),Vector2D(size.x/2,0),Vector2D(0,-size.y/2), Vector2D(0,size.y/2)};
	bool result = false;
	for(int i=0; i < 4; i++)
	{
		holdingControlPoint[i] = (spot|controlPoints[i])<0.2;
		result |= holdingControlPoint[i];
	}
	return result;
}
void TextureBox::Drag(Vector2D spot)
{
	if(holdingControlPoint[0]||holdingControlPoint[1])
	{
		size.x = fabs(spot.x)*2.0;
	}
	else if(holdingControlPoint[2]||holdingControlPoint[3])
	{
		size.y = fabs(spot.y)*2.0;
	}	
	float halfX = size.x/2;
	float halfY = size.y/2;
	vertices[0] = Vertex(Vector2D(-halfX, -halfY));
	vertices[1] = Vertex(Vector2D(-halfX, halfY));
	vertices[2] = Vertex(Vector2D(halfX, halfY));
	vertices[3] = Vertex(Vector2D(halfX, -halfY));
}
char * TextureBox::GetTag()
{
	printf("TextureBox::GetTag()\n");
	char * textureName = tile->GetTextureName();
	Vector2D * textureVertices = tile->GetTextureVertices();
	char * tag = (char*)malloc(sizeof(char)*(strlen(textureName)+75));
	sprintf(tag,"%s,(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f)",
		textureName,
		textureVertices[rotation%4].x,textureVertices[rotation%4].y,
		textureVertices[(1+rotation)%4].x,textureVertices[(1+rotation)%4].y,
		textureVertices[(2+rotation)%4].x,textureVertices[(2+rotation)%4].y,
		textureVertices[(3+rotation)%4].x,textureVertices[(3+rotation)%4].y,
		size.y, size.x);
	printf("Done TextureBox::GetTag() = %s\n", tag);
	return tag;
}
void TextureBox::SetTile(Tile * tile)
{
	this->tile = tile;
}
Tile * TextureBox::GetTile()
{
	return tile;
}
Vector2D TextureBox::GetSize()
{
	return size;
}
void TextureBox::RotateClockwise()
{
	rotation--;
	if(rotation < 0)
		rotation = 4+rotation;
}
void TextureBox::RotateAntiClockwise()
{
	rotation = (rotation+1)%4;
}
	